/**=========================================================
 * Controller: SystemJobController
  =========================================================*/
App.controller('SystemJobController', ['$scope', 'toaster', 'ngDialog', 'Utils', 'HttpUtils', 'DialogUtils', 'SimpleUiGrid',
    function($scope, toaster, ngDialog, utils, httpUtils, dialogUtils, simpleUiGridService) {
        //===========================jobGrid=================================            
        var jobGridColumns = [
            {
                displayName: '任务组',
                field: 'job_group'
            },
            {
                displayName: '任务名称',
                field: 'job_name'
            },
            {
                displayName: '处理类',
                field: 'class_name'
            },
            {
                displayName: '定时配置',
                field: 'cron',
                width: '150'
            },
            {
                displayName: '备注',
                field: 'remark'
            },
            {
                displayName: '启动状态',
                field: 'jobStatus',
                width: '100'
            },
            {
                displayName: '状态',
                field: 'status',
                width: '80'
            },
            { 
                name: '', 
                field: 'widgets', 
                width: '150',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.startJob(row.entity)" title="启动任务" class="btn btn-xs btn-success">\
                            <em class="fa fa-caret-square-o-right"></em>\
                        </button>\
                        <button ng-click="grid.appScope.updateCron(row.entity)" title="更新定时配置" class="btn btn-xs btn-warning">\
                            <em class="fa fa-clock-o"></em>\
                        </button>\
                        <button ng-click="grid.appScope.saveJob(row.entity)" title="保存" class="btn btn-xs btn-info">\
                            <em class="fa fa-save"></em>\
                        </button>\
                        <button ng-click="grid.appScope.deleteJob(row.entity)" title="删除" class="btn btn-xs btn-danger">\
                            <em class="fa fa-trash"></em>\
                        </button>\
                    </div>\
                '
            }
        ];
        
        $scope.queryParams = {};
        $scope.searchJobGrid = function() {
            jobGrid.draw($scope.queryParams);
        }; 

        var jobGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'jobGridOptions',
                pageOptionModel: 'jobGridPageOptions',
                gridApi: 'jobGridApi',
                url: '/sysJob/queryJob',
                pageType: 'common',
                pageSize: 25,
                columns: jobGridColumns,
                needExport: false,
                controllerSearchFn: $scope.searchjobGrid,
                menus: [
                    {
                        title: '新增',
                        action: function ($event) {
                            jobGrid.addRow({status: '1'},0);
                        }
                    }
                ]
            }
        );
        
        $scope.startJob = function(data){
            if (data.status == "0"){
                dialogUtils.alert('无法启动处于停用状态的任务');
            }else if (data.jobStatus == '已启动'){
                dialogUtils.alert('该任务已启动');
            }else{
                httpUtils.post('/sysJob/startJob',data,
                    function(result){
                        if (result.success){
                            data.jobStatus = '已启动';
                            toaster.pop('success', '', '启动成功');
                        }else{
                            toaster.pop('error', '', result.msg);
                        }
                    },
                    function(){
                        toaster.pop('error', '', '启动失败');
                    }
                );
            }
        };
        
        $scope.updateCron = function(data){
            if (data.status == '0'){
                dialogUtils.alert('无法更新处于停用状态的任务');
            }else if (data.jobStatus == '未启动'){
                dialogUtils.alert('该任务尚未启动');
            }else{
                httpUtils.post('/sysJob/updateJobCron',data,
                    function(result){
                        if (result.success){
                            toaster.pop('success', '', '更新成功');
                        }else{
                            toaster.pop('error', '', result.msg);
                        }
                    },
                    function(){
                        toaster.pop('error', '', '更新失败');
                    }
                );
            }
        };
        
        $scope.saveJob = function(data){
            httpUtils.post('/sysJob/save',data,
                function(result){
                    if (result.success){
                        if (utils.isEmpty(data.tid)){
                            data.tid = result.msg;
                        }
                        toaster.pop('success', '', '保存成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '保存失败');
                }
            );
        };
        
        $scope.deleteJob = function(data){
            dialogUtils.confirm('确定要删除吗?',
                function () {
                    httpUtils.post('/sysJob/delete',data,
                        function(result){
                            if (result.success){
                                jobGrid.deleteRow(data);
                                toaster.pop('success', '', '删除成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '删除失败');
                        }
                    );
                }
            );       
        };

        
//===========================init================================= 
        $scope.searchJobGrid();
    }
]);
